<template>
  <div id="Login">
    <form action="" class="login">
      <p>Login</p>
      <input type="text" placeholder="用户名" v-model="loginForm.username"/>
      <input type="password" placeholder="密码" v-model="loginForm.password" />
      <input type="submit" class="btn" value="登  录"  @click.prevent="login"/>
    </form>
  </div>
</template>
<script>
import { post_json } from "../utils/request.js";
import { mapMutations } from 'vuex';
export default {
  data() {
    return {
      loginForm: {
        username: "Diana",
        password: "123321",
      },
      userToken:''
    };
  },
  methods: {
    ...mapMutations(['changeLogin']),
    login() {
      post_json("user/login", this.loginForm).then((res) => {
        // console.log(res);
        // console.log(res.data.token);
        if (res.status == 200) {
          this.userToken = res.data.token
          // 将用户token保存到vuex中
          this.changeLogin({ Authorization: this.userToken });
          // 跳转页面
          this.$router.push({ path: "/home" });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
#Login{
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background-image: url(../assets/background.jpeg);
}
.login {
  position: absolute;
  top: 50%;
  margin-top: -200px;
  left: 50%;
  margin-left: -200px;
  /* absolute居中的一种方法 */
  background-color: whitesmoke;
  width: 400px;
  height: 400px;
  border-radius: 25px;
  text-align: center;
  padding: 5px 40px;
  box-sizing: border-box;
  /* 这样padding就不会影响大小 */
}

p {
  font-size: 42px;
  font-weight: 600;
}

input {
  background-color: whitesmoke;
  width: 100%;
  height: 48px;
  margin-bottom: 10px;
  border: none;
  border-bottom: 2px solid silver;
  /* 下面的会覆盖上面的步伐 */
  outline: none;
  font-size: 22px;
}

.btn {
  background-color: #59c2c5;
  width: 38%;
  height: 48px;
  border-radius: 8px;
  margin-top: 40px;
  font-size: 28px;
  font-weight: 600;
  color: white;
}
.btn:hover {
  background-color: #59c2a0;
}
</style>